/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import com.sun.org.apache.xpath.internal.functions.Function;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import model.Calculation;
import model.Formula;
import view.FunctionGUI;
import view.StartGUI;




/**
 *
 * @author Stupi
 */
public class Controller {
    
    private StartGUI startGUI;
    private FunctionGUI functionGUI;

    public Controller() {
        startGUI = new StartGUI();
        startGUI.setVisible(true);
        
        startGUI.addButtonListener(new DrawListener());
    }
    
    class DrawListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent ae) {
            
            
            
            int ratio = 50;
            
            int xPlus = Math.abs(startGUI.getXplus());
            int xMinus = xPlus * -1;
            
            int yPlus = Math.abs(startGUI.getYplus());
            int yMinus = yPlus * -1;
            
            int length = (xPlus-xMinus)*ratio;
            int [] xValues = new int [length];
            int [] yValues = new int [length];
            
            String formula = startGUI.getFormula();
            //ovde ide provera
            
           /* Calculation calc = new Calculation();
            calc.setFormula(formula);
            */
            
            Formula form = new Formula();
            form.setFormula(formula);
            
            for (int i = xMinus*ratio; i < xPlus*ratio; i++) {
                double x = i*1.0;
                x = x/ratio;
                
                form.calculateYValue(x);
                double y = form.getyValue();
                
                int xCoo = i;
                int yCoo = (int) Math.round(y*ratio);
                
                
                
                int position = i-xMinus*ratio;
                
                xValues[position] = ((xPlus - xMinus)*ratio/2) + xCoo;
                yValues[position] = ((yPlus - yMinus)*ratio/2) - yCoo;

            }
            
            functionGUI = new FunctionGUI(xValues, yValues, xMinus, xPlus, yMinus, yPlus);
            functionGUI.setSize(xPlus, yPlus);
            functionGUI.setVisible(true);
                
           
            
        }
        
    }
    
    
}
